package top.fullj;

import java.security.SecureRandom;
import java.util.Random;

/**
 * @author bruce.wu
 * @since 2021/8/3 14:10
 */
public abstract class RandomString {

    private static final String CHARS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

    private static final Random RND = new SecureRandom();

    public static String random(int count) {
        return random(count, CHARS, 0, CHARS.length());
    }

    public static String randomNumeric(int count) {
        return random(count, CHARS, 0, 10);
    }

    public static String randomAlphabetic(int count) {
        return random(count, CHARS, 10, 52);
    }

    public static String randomAlphanumeric(int count) {
        return random(count, CHARS, 0, CHARS.length());
    }

    public static String random(int count, String chars) {
        return random(count, chars, 0, chars.length());
    }

    public static String random(int count, String chars, int offset, int length) {
        StringBuilder sb = new StringBuilder(count);
        for (int i = 0; i < count; i++) {
            sb.append(chars.charAt(offset + RND.nextInt(length)));
        }
        return sb.toString();
    }

}
